package com.aid.winter.service.impl;

import com.aid.winter.common.exception.BusinessException;
import com.aid.winter.dto.request.ClothesRegisterRequest;
import com.aid.winter.dto.response.ClothesRegisterVO;
import com.aid.winter.dto.response.PageResult;
import com.aid.winter.entity.AidApplication;
import com.aid.winter.entity.AidClothesRegister;
import com.aid.winter.entity.AidClothesStyle;
import com.aid.winter.mapper.AidApplicationMapper;
import com.aid.winter.mapper.AidClothesRegisterMapper;
import com.aid.winter.mapper.AidClothesStyleMapper;
import com.aid.winter.service.ClothesRegisterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class ClothesRegisterServiceImpl implements ClothesRegisterService {

    private final AidClothesRegisterMapper registerMapper;
    private final AidClothesStyleMapper styleMapper;
    private final AidApplicationMapper applicationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ClothesRegisterVO register(Long studentId, ClothesRegisterRequest request) {
        // 验证申请是否存在且通过审核
        AidApplication application = applicationMapper.selectById(request.getApplicationId());
        if (application == null) {
            throw new BusinessException("申请不存在");
        }
        if (!studentId.equals(application.getStudentId())) {
            throw new BusinessException("无权操作他人的申请");
        }
        if (application.getStatus() != 4) { // 4-学校审核通过
            throw new BusinessException("申请未通过审核");
        }

        // 验证是否已经登记过
        AidClothesRegister existRegister = registerMapper.selectByApplicationId(request.getApplicationId());
        if (existRegister != null) {
            throw new BusinessException("该申请已经登记过尺码");
        }

        // 验证款式是否存在且可用
        AidClothesStyle style = styleMapper.selectById(request.getStyleId());
        if (style == null || style.getStatus() != 1) {
            throw new BusinessException("款式不存在或已禁用");
        }

        // 创建登记记录
        AidClothesRegister register = new AidClothesRegister();
        BeanUtils.copyProperties(request, register);
        register.setStudentId(studentId);
        register.setStatus(1);

        registerMapper.insert(register);

        return getRegisterDetail(register.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ClothesRegisterVO updateRegister(Long id, Long studentId, ClothesRegisterRequest request) {
        AidClothesRegister register = getRegisterById(id);

        // 验证是否是本人的登记
        if (!studentId.equals(register.getStudentId())) {
            throw new BusinessException("无权修改他人的登记信息");
        }

        // 验证款式是否存在且可用
        AidClothesStyle style = styleMapper.selectById(request.getStyleId());
        if (style == null || style.getStatus() != 1) {
            throw new BusinessException("款式不存在或已禁用");
        }

        BeanUtils.copyProperties(request, register);
        registerMapper.updateById(register);

        return getRegisterDetail(id);
    }

    @Override
    public ClothesRegisterVO getRegisterDetail(Long id) {
        return convertToVO(getRegisterById(id));
    }

    @Override
    public PageResult<ClothesRegisterVO> listRegisters(Long styleId, Long studentId, Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;

        List<AidClothesRegister> registers = registerMapper.selectList(styleId, studentId, offset, pageSize);
        long total = registerMapper.selectCount(styleId, studentId);

        List<ClothesRegisterVO> voList = registers.stream()
                .map(this::convertToVO)
                .toList();

        return PageResult.build(voList, total, pageSize, pageNum);
    }

    @Override
    public List<Map<String, Object>> getSizeStatistics(Long styleId, Long collegeId) {
        return registerMapper.selectSizeStatistics(styleId, collegeId);
    }

    private AidClothesRegister getRegisterById(Long id) {
        AidClothesRegister register = registerMapper.selectById(id);
        if (register == null) {
            throw new BusinessException("登记记录不存在");
        }
        return register;
    }

    private ClothesRegisterVO convertToVO(AidClothesRegister register) {
        ClothesRegisterVO vo = new ClothesRegisterVO();
        BeanUtils.copyProperties(register, vo);

        // 查询款式信息
        AidClothesStyle style = styleMapper.selectById(register.getStyleId());
        if (style != null) {
            vo.setStyleName(style.getStyleName());
        }

        // 查询学生信息
        AidApplication application = applicationMapper.selectById(register.getApplicationId());
        if (application != null) {
            vo.setStudentName(application.getStudentName());
        }

        return vo;
    }
}